$(document).ready(function(e){ 
	createPageTab();
	createBrandListTab();
	findBrandByPage(1);
});

var totalPageNum = 0;
var isDesc = "desc";
var brandList = null;
var curPageNum = null;
function findBrandByPage(pageNow){
	var tmp2=root+"/admin/brand!findBrandByPage";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "isDesc": isDesc, "page.numberPerPage": 20},
		dataType:"json",
		success: function(json){
			//创建表格头信息
			if(brandList!=null) createBrandListTab();
			
			totalPageNum = json.totalPageNum;
			brandList = json.pageData;
			curPageNum = json.curPageNum;
			//写入系统list
			var htmlStr = '';
			for(var i=0; i<json.pageData.length; i++){
				htmlStr += '<tr><td height="24" bgcolor="#FFFFFF"><div align="center" class="STYLE1">';
				htmlStr += '<input name="checkbox" type="checkbox" class="STYLE2" value="'+json.pageData[i].id+'" />';
				htmlStr += '</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF" class="STYLE2">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+((json.curPageNum-1)*20+(i+1))+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].name+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].content+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].createDate.replace("T"," ")+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1"><a href="'+root+'/admin/product!goProductList?brand.id='+json.pageData[i].id+'">品牌商品</a></div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center">';
				htmlStr += '<img src="'+root+'/admin/company/images/037.gif" width="9" height="9" />';
				htmlStr += '<span class="STYLE1"> [</span><a href="javascript:;" onclick="showUpdateBrandDiv('+json.curPageNum+','+i+')">编辑</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center"><span class="STYLE2">';
				htmlStr += '<img src="'+root+'/admin/company/images/010.gif" width="9" height="9" />';
				htmlStr += '</span><span class="STYLE1">[</span><a href="javascript:;" onclick="deleteCompany('+json.pageData[i].id+')">删除</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td></tr>';
			}
			$("#listInfoTab").append(htmlStr);
			//写入分页信息
			$("#pageInfo").html('共'+json.totalCount+'条纪录，当前第'+json.curPageNum+'/'+json.totalPageNum+'页，每页'+json.numberPerPage+'条纪录');
			$("#onePage").unbind("click");
			$("#onePage").bind("click",function(){findBrandByPage(1);});
			if(json.curPageNum>1){
				$("#perPage").unbind("click");
				$("#perPage").bind("click",function(){findBrandByPage((json.curPageNum-1));});
			}
			if((json.curPageNum+1)<=json.totalPageNum){
				$("#nextPage").unbind("click");
				$("#nextPage").bind("click",function(){findBrandByPage((json.curPageNum+1));});
			}
			$("#lastPage").unbind("click");
			$("#lastPage").bind("click",function(){findBrandByPage(json.totalPageNum);});
		}
	});
}

function goPage(){
	var p = $("#pageNum").val();
	if(goPage_v(p)){
		findBrandByPage(p);
	}
}

function showAddBrandDiv(){
	$("#backGroud").show();
	$("#addDiv").show();
}

function closeAddBrandDiv(){
	$("#addBrandForm")[0].reset();
	$("#backGroud").hide();
	$("#addDiv").hide();
}

function saveBrand(){
	if($("#brandName").val()!="" && $("#brandName").val()!=""){
		$("#addBrandForm").submit();
	}else{
		alert("系统名跟系统简介不能为空！");
	}
}

function showUpdateBrandDiv(pageNow, index){
	$("#backGroud").show();
	$("#updateDiv").show();
	$("#brandId_u").val(brandList[index].id);
	$("#brandName_u").val(brandList[index].name);
	$("#brandContent_u").val(brandList[index].content);
	$("#updateBrandButton").unbind("click");
	$("#updateBrandButton").bind("click",function(){updateBrand(pageNow);});
}

function closeUpdateBrandDiv(){
	$("#brandId_u").val("");
	$("#brandName_u").val("");
	$("#brandContent_u").val("");
	
	$("#backGroud").hide();
	$("#updateDiv").hide();
}

function updateBrand(pageNow){
	var tmp2=root+"/admin/brand!updateBrand";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"brand.id":$("#brandId_u").val(),"brand.name":$("#brandName_u").val(), "brand.content": $("#brandContent_u").val()},
		dataType:"json",
		success: function(json){
			closeUpdateBrandDiv();
			if(json.code=="y"){
				findBrandByPage(pageNow);
			}else{
				alert("修改失败，请稍后重试！");
			}
		}
	})
}

/**
 * 删除品牌
 * @param {Object} brandID
 * @param {Object} pageNow
 */
function deleteCompany(brandID){
	var ids = "";
	if(brandID==null){
		ids = getCheckBoxVal();
		if(ids==""){
			alert("请至少选择一个品牌！");
			return;
		}
	}
	
	var tmp2=root+"/admin/brand!deleteBrand";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"ids":ids, "brand.id":brandID,"page.curPageNum":curPageNum, "isDesc": isDesc},
		dataType:"json",
		success: function(json){
			$("#ck").removeAttr("checked");
			findBrandByPage(json.curPageNum);
		}
	})
}